package sysman.action;

import java.util.List;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springside.core.web.StrutsEntityAction;
import org.apache.struts.validator.LazyValidatorForm;

import sysman.dao.BacklogDao;
import sysman.model.Backlog;
import sysman.model.User;
import sysman.service.ISysService;

import common.Globals;
import common.PageInfo;


public class BacklogAction extends StrutsEntityAction<Backlog, BacklogDao> {

	private BacklogDao backlogDao;
	private ISysService sysService;
	
	public BacklogDao getBacklogDao() {
		return backlogDao;
	}

	public void setBacklogDao(BacklogDao backlogDao) {
		this.backlogDao = backlogDao;
	}

	/**
	 *列表
	 */
	@Override
	public ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		LazyValidatorForm lvForm = (LazyValidatorForm)form;
		PageInfo pageInfo = new PageInfo(request);
		HashMap<String, String> hm = new HashMap<String, String>();
		//TODO 组装查询条件
		//获取当前人
		User currentUser = sysService.getUser(request);
		hm.put("userId" ,currentUser.getId().toString());
		
		String title = (String)lvForm.get("s_title");
		hm.put("title" ,title);
		String status = (String)lvForm.get("s_status");
		hm.put("status" ,status);
		String type = (String)lvForm.get("s_type");
		hm.put("type" ,type);
		
		String sDate = (String)lvForm.get("s_Date");
		hm.put("sDate", sDate);
		lvForm.set("s_Date", sDate);
		
		String eDate = (String)lvForm.get("e_Date");
		hm.put("eDate", eDate);
		lvForm.set("e_Date", eDate);
		
		request.setAttribute("userId", ","+currentUser.getId().toString()+"&");	
		List<Backlog> poList = this.backlogDao.findList(hm, pageInfo);
		request.setAttribute("list", poList);
		request.setAttribute("status", status);
		request.setAttribute("sDate", sDate);
		request.setAttribute("eDate", eDate);
		
		String url="";
		String urlName1 = "";
		String urlName2 ="";
		String urlName3 ="";
		if(status!=null&&"10".equals(status)){
			urlName1="> 待办待阅";
			urlName2="> 我的待办";
			urlName3="我的待办";
			url = "/BacklogAction.do?s_status=10";
		}else if(status!=null&&"20".equals(status)){
			urlName1="> 待办待阅";
			urlName2="> 我的待阅";
			urlName3="我的待阅";
			url = "/BacklogAction.do?s_status=10";
		}else if(status!=null&&"11".equals(status)){
			urlName1="> 已办已阅";
			urlName2="> 我的已办";
			urlName3="我的已办";
			url = "/BacklogAction.do?s_status=10";
		}else if(status!=null&&"21".equals(status)){
			urlName1="> 已办已阅";
			urlName2="> 我的已阅";
			urlName3="我的已阅";
			url = "/BacklogAction.do?s_status=10";
		}
		request.setAttribute("urlName1", urlName1);
		request.setAttribute("urlName2", urlName2);
		request.setAttribute("urlName3", urlName3);
		return mapping.findForward(Globals.FORWARD_LIST);
	}
	
	/**
	 * 删除
	 */
	@Override
	public ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		super.delete(mapping, form, request, response);
		
		return this.list(mapping, form, request, response);
	}

	/**
	 * 保存
	 */
	@Override
	public ActionForward save(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {		
		super.save(mapping, form, request, response);
		
		return this.list(mapping, form, request, response);
	}
	
	/**
	 * 该方法被delete方法调用
	 */
	@Override
	protected void doDeleteEntity(ActionForm form, HttpServletRequest request) {
		String ids = request.getParameter("ids");
		try {
			if (ids != null && !"".equals(ids.trim())) {
				getEntityManager().deleteByIds(ids);
				// TODO 修改操作事件，参考logWrite方法要求
				//logsManager.logWrite(request, "", "操作日志", 0);
			}
		} catch (Exception e) {
			// TODO 修改操作事件，参考logWrite方法要求
			//logsManager.logWrite(request, "", "操作日志", 0);
		}
	}
	
	/**
	 * 结束代办
	 */
	public ActionForward closeBacklog(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		String id = request.getParameter("id");
		Backlog obj = this.getBacklogDao().findUniqueBy("id", Long.parseLong(id));
		obj.setStatus("11");
		this.getBacklogDao().save(obj);
		 request.setAttribute("url", "BacklogAction.do?method=list");
		 return mapping.findForward("result");
	}
	
	public ActionForward listUser(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) {
		LazyValidatorForm lvForm = (LazyValidatorForm)form;
		PageInfo pageInfo = new PageInfo(request);
		HashMap<String, String> hm = new HashMap<String, String>();
		String type ="";
		//TODO 组装查询条件
		//获取当前人
		hm.put("userId" ,request.getParameter("userId"));
		
		String title = (String)lvForm.get("s_title");
		hm.put("title" ,title);
		String status = (String)lvForm.get("s_status");
		hm.put("status" ,status);
		
		String sDate = (String)lvForm.get("s_Date");
		hm.put("sDate", sDate);
		
		String eDate = (String)lvForm.get("e_Date");
		hm.put("eDate", eDate);
		
		if("20".equals(status)||"21".equals(status)){
			type="0";
		}else if("10".equals(status)||"11".equals(status)){
			type="1";
		}
		hm.put("type" ,type);
		request.setAttribute("uid", request.getParameter("userId"));	
		request.setAttribute("userId", ","+request.getParameter("userId")+"&");	
		List<Backlog> poList = this.backlogDao.findList(hm, pageInfo);
		request.setAttribute("list", poList);
		
		
		
		
		return mapping.findForward("listUser");
	}
	
	//==============================================二期==========================================
	/**
	 * 加载左边的菜单树
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward loadTreeMenu(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response){
		String parentId = request.getParameter("parentId");
		
		return new ActionForward("/purview.do?method=treeMenu&parentId=" + parentId);
	}
}


